package com.yueya.business.bed.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.yueya.common.util.UUIDGenerator;

@SuppressWarnings("serial")
public class House extends Model<House> {
	
	private List<Floor> floors ;
	
	public static House dao = new House();
	
	
	public boolean save(House dao){
		dao.set("house_id", UUIDGenerator.getUUID());
		return dao.save();
	}
	
	public boolean update(House dao){
		return dao.update();
	}
	
	public boolean remove(House dao){
		dao.set("del_flag","1");
		return dao.update();
	}
	
	//栋编号
	public static  String no() {
		String number ="";
		String tag ="H";
		long count = 1 + Db
				.queryLong("SELECT COUNT(house_id)  FROM jg_house ");
		if (0 < count) {
			if (10 > count)
				number = tag + "000" + count;
			else if (100 > count)
				number = tag + "00" + count;
			else if (1000 > count)
				number = tag + "0" + count;
			else
				number = tag + count;
		}
	   
		return number;
	}
	
	

	 public List<House> getList() {
		 return House.dao.find("select * from jg_house order by house_no");
	 }
	 
	 public List<House> getList(String org_id) {
		 return House.dao.find("select * from jg_house where org_id=? order by house_no ",org_id);
	 }
	 
	 public List<Map<String, Object>>  getAllList(String org_id) {
		    List<Map<String, Object>>  result = new ArrayList<Map<String, Object>>();
		    List<House>  list = getList(org_id);
		    Iterator<House> it = list.iterator();
		    while(it.hasNext()) {
		    	Map<String, Object> map =new HashMap<String, Object>();
		        House house = it.next();
		        map.put("id", house.getStr("house_id"));
		        map.put("name", house.getStr("house_name"));
		        List<Map<String, Object>> listfloor =	Floor.dao.getAllList(house.getStr("house_id"));
		        map.put("floors", listfloor);
		        result.add(map);
		     }
		 return result;
	 }
	 
	 public List<Map<String, Object>> getAllList() {
		    List<Map<String, Object>>  result = new ArrayList<Map<String, Object>>();
		    List<House>  list = getList();
		    Iterator<House> it = list.iterator();
		    while(it.hasNext()) {
		    	Map<String, Object> map =new HashMap<String, Object>();
		        House house = it.next();
		        map.put("id", house.getStr("house_id"));
		        map.put("name", house.getStr("house_name"));
		        List<Map<String, Object>> listfloor =	Floor.dao.getAllList(house.getStr("house_id"));
		        map.put("floors", listfloor);
		        result.add(map);
		     }
		 return result;
	 }

	public List<Floor> getFloors() {
		return floors;
	}

	public void setFloors(List<Floor> floors) {
		this.floors = floors;
	}
	
	

}
